package com.sbtr.controller.classify;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.business.equip.entity.EquipEntity;
import com.sbtr.common.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sbtr.exception.BDException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.sbtr.common.PageDto;
import com.sbtr.business.classify.entity.EquipCatalog;
import com.sbtr.business.classify.dto.EquipCatalogDto;
import com.sbtr.business.classify.service.EquipCatalogService;
import org.springframework.web.bind.annotation.RestController;
import com.sbtr.controller.common.CommonController;

import javax.servlet.http.HttpServletRequest;

/**
 * 设备型号 前端控制器
 *
 * @author dgl
 * @since 2023-09-15
 */
@RestController
@RequestMapping("/equipCatalog")
@Api(tags = "设备型号")
public class EquipCatalogController extends CommonController<EquipCatalog,EquipCatalogService> {

    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "分页列表")
    public Response<PageDto<EquipCatalog>> pageList(EquipCatalogDto equipCatalogDto, HttpServletRequest request){
        String algorithmUserId = request.getHeader("algorithmUserId");
        equipCatalogDto.setCreater(algorithmUserId);

        return Response.success(u.pageList(equipCatalogDto));
    }

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "保存")
    public Response<EquipCatalog> save(@RequestBody EquipCatalog equipCatalog, HttpServletRequest request){
        String algorithmUserId = request.getHeader("algorithmUserId");

        QueryWrapper<EquipCatalog> wrapper = new QueryWrapper<>();
        wrapper.eq("type_code",equipCatalog.getTypeCode());
        int count = equipCatalog.selectCount(wrapper);
        if(count>0){
            throw new BDException("已经存在该设备编号");
        }
        equipCatalog.setId(null);
        equipCatalog.setCreater(algorithmUserId);
        if(equipCatalog.insert()){
            return Response.success(equipCatalog);
        }else throw new BDException("保存失败");
    }

}

